package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.util.List;

@RestController
@RequestMapping("/user")
public class TestController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public User login(@RequestBody User user) {
        //       if (StringUtils.isBlack(username)) return "用户名不能为空";
        //     if (StringUtils.isBlack(pwd))return "密码不能为空";
        User users = userService.login(user.getUsername(), user.getPwd());
        return users;
    }

    @GetMapping("/add")
    public String add(String username, String pwd, Integer age) {
        if (StringUtils.isBlack(username)) return "用户名不能为空";
        if (StringUtils.isBlack(pwd)) return "密码不能为空";
        if (age != null && age <= 0) return "年龄输入错误";
        Boolean res = userService.add(username, pwd, age);
        if (res == true) return "添加成功";
        return "添加失败";

    }

    @GetMapping("/getAllUser")
    public List<User> getAllUser() {
        return userService.getAllUser();
    }

    //通过username删除用户
    @GetMapping("/deleteUserByName")
    public String deleteUserByName(String username) {
        if (StringUtils.isBlack(username)) return "用户名不能为空";
       return userService.deleteUserByName(username);
    }

    @RequestMapping(value = "/modifyUserByName")
    public String modifyUserByName(String username, String pwd, Integer age) {
        if (StringUtils.isBlack(username)) return "用户名不能为空";
        if (StringUtils.isBlack(pwd)) return "密码不能为空";
        if (age == null || age <= 0) return "年龄输入错误";
        return userService.modifyUserByName(username, pwd, age);
    }
    //通过username修改用户
}

